import WebSocketService from '@/utils/webSocket';//引入websocket.ts文件
import { useUserStore } from '@/stores/user';
import { Config } from '@/config' //引入公共配置文件
import { ref, onMounted, onUnmounted } from 'vue';
import type { socketDataObj } from '@/apis/supply/type'
/**
 * webSocket连接以及数据获取钩子
 * @returns { object }  socketData-收到的数据
 */
export default function () {
  let userStore = useUserStore();
  const wsOptions = {
    url: Config.WEB_BASE_URL2 + userStore.sid,
    isOpenHeartCheck: false,
    iSReconnect: true,
  };

  const ws = new WebSocketService(wsOptions.url, wsOptions.isOpenHeartCheck, wsOptions.iSReconnect);
  const socketData = ref<socketDataObj>();
  onMounted(() => {
    ws.connect();
    ws.on('message', (data) => {
      socketData.value = data;
    })
  });
  onUnmounted(() => {
    ws.close();
  })
  return { socketData }
}